#!/usr/bin/env bash

logfile=$1
rotateDays=$2
if [[ ! -f ${logfile} ]]; then
    echo "logfile: \"${logfile}\" not exists, exit."
    exit
fi
if [[ "${rotateDays}"X == "X" ]]; then
   rotateDays=15
fi

logfileName=$(basename ${logfile})
logfileDir=$(dirname $(readlink -f ${logfile}))

cd ${logfileDir}
for ((i=${rotateDays};i>=1;i--)); do
  [[ -f ${logfileName}.${i} ]] && mv -f ${logfileName}.${i} ${logfileName}.$((${i}+1))
done

cp ${logfileName} ${logfileName}.1 && > $logfileName
